import { useUserStore } from "@/store";
import request, { get, post } from "@/utils/request";

/*
 * @Author: anxia.ack anchenkai@come-future.com
 * @Date: 2025-04-18 10:55:23
 * @LastEditors: anxia.ack anchenkai@come-future.com
 * @LastEditTime: 2025-04-20 12:15:29
 * @FilePath: /free-learning-front/src/api/user-course.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
export interface ByUserQuery {
  status: number;
  keyword: string;
  categoryId: number;
}
export interface UserCourse {
  id: number;
  userId: number;
  userName: string;
  courseId: number;
  courseName: string;
  consultantId: number;
  consultantName: string;
  progress: number;
  learningDuration: number;
  status: number;
  lastLearnTime: string;
  currentChapter: number;
  categoryId: number;
  categoryName: string;
  createTime: string;
  updateTime: string;
  coverImage: string;
}
const BASE_URL = "/api/v1/userCourse";
export const queryByUser = (query: Partial<ByUserQuery>): Promise<UserCourse[]> => {
  return post(`${BASE_URL}/queryByUser`, query);
};

const userStore = useUserStore();
export const addCourse = (
  userCourse: Partial<UserCourse>
): Promise<boolean> => {
  userCourse.userId = userStore.userInfo?.userId;
  userCourse.userName = userStore.userInfo?.nickname;
  return post<boolean>(`${BASE_URL}/add`, userCourse);
};

export const deleteById = (id: number): Promise<boolean> => {
  return request.delete(`${BASE_URL}/delete${id ? `/${id}` : ""}`);
};

export const updateCourseUser = (
  userCourse: Partial<UserCourse>
): Promise<boolean> => {
  return request.put(`${BASE_URL}/update`, userCourse);
};

export const checkUserHasCourse = (
  userCourse: Partial<UserCourse>
): Promise<boolean> => {
  return post(`${BASE_URL}/checkUserHasCourse`, userCourse);
};

export const deleteByUserIdAndCourseId = (
  userCourse: Partial<UserCourse>
): Promise<boolean> => {
  return post(`${BASE_URL}/deleteByUserIdAndCourseId`, userCourse);
};
export interface CourseChapters {
  id: number; // 主键ID
  courseId: number; // 课程ID
  chapterName: string; // 章节标题
  chapterOrder: number; // 章节序号
  chapterDes: string; // 章节描述
  chapterUrl: string; // 章节URL
  fileName: string; // 文件名
  progress: number; // 观看进度
}
export interface UserCourseWatchVO extends UserCourse {
  chaptersNum: number;
  isLike: boolean;
  likeCount: number;
  chapters: CourseChapters[];
}
export const queryUserCourseWatchStatus = (
  courseId: number
): Promise<UserCourseWatchVO> => {
  return get(`${BASE_URL}/queryUserCourseWatchStatus/${courseId}`);
};

export interface UserCourseChapter {
  id: number;
  userId: number;
  chapterId: number;
  progress: number;
  courseId: number;
}

export const queryChapterProgress = (chapterId: number): Promise<UserCourseChapter> => {
  return get(`${BASE_URL}/queryChapterProgress/${chapterId}`);
};


export const updateProgress = (userCourseChapter: Partial<UserCourseChapter>): Promise<boolean> => {
  return post(`${BASE_URL}/updateProgress`, userCourseChapter);
};


export const likeCourse = (courseId: number): Promise<boolean> => {
  return post(`${BASE_URL}/like/${courseId}`);
};

